package zhoug.zui.utils;

import android.graphics.Bitmap;

import zhoug.logging.Logger;

import java.text.MessageFormat;

/**
 * @Author 35574
 * @Date 2020/7/29
 * @Description
 */
public class ZUIBitmapUtils {
    private static final String TAG = ">>>ZUIBitmapUtils";
    private static  boolean debug = false;

    public static void setDebug(boolean debug) {
        ZUIBitmapUtils.debug = debug;
    }

    /**
     * 剪切图片,清除四周的空隙（背景色）
     * @param bitmap 原图
     * @param backColor  背景色
     * @param blank  边界保留的尺寸
     * @return
     */
    public static Bitmap clipBlank(Bitmap bitmap, int backColor, int blank) {
        if (blank < 0) {
            blank = 0;
        }
        int WIDTH = bitmap.getWidth();
        int HEIGHT = bitmap.getHeight();
        int top = 0, left = 0, right = WIDTH, bottom = HEIGHT;
        int[] pixs = new int[WIDTH];
        //扫描上边距不等于背景颜色的第一个点
        for (int y = 0; y < HEIGHT; y++) {
            //这里，x, y, width, height是属于从哪里来的参数，也就是我们控制怎样读取mBitmap1的参数；
            //offset, stride是到哪里去的参数，也就是控制如何放入到pixels[]中去的参数；
            //相信这里你已经大概明白了不少：offset是目标内存的起始地址的偏移量，stride是目标内存中隔多少个Pixels再写下一行；
            //取第y行的像素点
            bitmap.getPixels(pixs, 0, WIDTH, 0, y, WIDTH, 1);
            boolean find = false;
            for (int p : pixs) {
                if (p != backColor) {
                    top = y;
                    find = true;
                    break;
                }
            }
            if (find) {
                break;
            }
        }
        //扫描下边
        for (int y = HEIGHT - 1; y >= 0; y--) {
            bitmap.getPixels(pixs, 0, WIDTH, 0, y, WIDTH, 1);
            boolean find = false;
            for (int p : pixs) {
                if (p != backColor) {
                    bottom = y;
                    find = true;
                    break;
                }
            }
            if (find) {
                break;
            }
        }
        //扫描左边,
        //上下已经扫描过的不需要再重复扫描了
        int h2 = bottom - top;
        if (debug) {
            Logger.d(TAG, "clipBlank:h2=" + h2);
        }
        pixs = new int[h2];
        for (int x = 0; x < WIDTH; x++) {
            bitmap.getPixels(pixs, 0, 1, x, top, 1, h2);
            boolean find = false;
            for (int p : pixs) {
                if (p != backColor) {
                    left = x;
                    find = true;
                    break;
                }
            }
            if (find) {
                break;
            }
        }
        //扫描右边,
        for (int x = WIDTH - 1; x >= 0; x--) {
            bitmap.getPixels(pixs, 0, 1, x, top, 1, h2);
            boolean find = false;
            for (int p : pixs) {
                if (p != backColor) {
                    right = x;
                    find = true;
                    break;
                }
            }
            if (find) {
                break;
            }
        }

        if (debug) {
            Logger.d(TAG, "clipBlank:" + MessageFormat.format("left={0},top={1},right={2},bottom={3}", left, top, right, bottom));
        }
        left -= blank;
        top -= blank;
        right += blank;
        bottom += blank;
        if (left < 0) {
            left = 0;
        }
        if (top < 0) {
            top = 0;
        }
        if (right > WIDTH) {
            right = WIDTH;
        }
        if (bottom > HEIGHT) {
            bottom = HEIGHT;
        }
        return Bitmap.createBitmap(bitmap, left, top, right - left, bottom - top);
    }

}
